/**
 * Class for orders page in admin part
 */
function ContentFilter(){
    
    /**
     * Sort column
     */
    var sortColumn = null;
    
    /**
     * Sort direction
     */
    var sortDirection = null;
    
    /**
     * Current object
     */
    var self = this;
    
    /**
     * Load filtered orders
     */
    this.loadFiltered = function (){
        var formUrl = jQuery(location).attr('href').replace('#','')+'?'+jQuery('#filter-form').serialize();
        jQuery.ajax({
            url: formUrl,
            type: 'POST',
            success: function(data) {
                jQuery('.filtered-content').html(data);
                if (sortColumn != null){
                    if (sortDirection == "asc"){
                        jQuery('[name="'+sortColumn+'"]').attr('title','desc');
                    }
                    else{
                        jQuery('[name="'+sortColumn+'"]').attr('title','asc');
                    }
                }
                jQuery('.column-sort').click(self.sortFiltered);
            }
        });
        return false;
    }

    /**
     * Load sorted orders
     */
    this.sortFiltered = function(){
        sortColumn=jQuery(this).attr('name');
        sortDirection = jQuery(this).attr('title');
        jQuery('[name="sort"]').attr('value',sortColumn);
        jQuery('[name="direction"]').attr('value',sortDirection);
        self.loadFiltered();

    }

    /**
     * Load sorted orders
     */
    this.getFiltered = function(){
        jQuery('[name="page"]').attr('value',1);
        self.loadFiltered();
    }
}

/**
 * Initialize document
 */
jQuery(document).ready(
    function(){
        var contentFilter = new ContentFilter();
        jQuery('#filter-form').submit(contentFilter.getFiltered);
        jQuery('.column-sort').click(contentFilter.sortFiltered);
        jQuery( ".date-field" ).datepicker({
            showOn: "button",
            buttonImage: "/images/calendar.gif",
            buttonImageOnly: true
        });
    }
    );
